﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibEBML.Common;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace MKVSplitter
{
    class Program
    {
        static void Main(string[] args)
        {
            //TestMKV();
            TestRegex();
        }

        private static void TestMKV()
        {
            string Filename = @"f:\videos\[SumiSora][ToHeart2_adnext][OVA][02][X264_AAC](8D9B3CA9).mkv";

            Stopwatch sw = new Stopwatch();
            using (var reader = System.IO.File.OpenRead(Filename))
            {
                using (var buffer = new System.IO.BufferedStream(reader))
                {
                    sw.Start();
                    EBMLDocument document = new EBMLDocument(buffer);
                    sw.Stop();
                    Console.WriteLine("Read DocumentTree:{0}ms", sw.ElapsedMilliseconds);
                    sw.Restart();
                    Matroska mts = new Matroska(document);
                    sw.Stop();
                    Console.WriteLine("Read MatroskaTree:{0}ms", sw.ElapsedMilliseconds);
                    //sw.Restart();
                    Segment seg = mts.Segment;
                    var cs = seg.Clusters;
                    long l1 = 0;
                    //foreach (var item in cs)
                    var item = cs[0];
                    {
                        var bs = item.Blocks.Where(m => m.TrackNumber == 1);

                        // foreach (var item1 in bs)
                        var item1 = bs.FirstOrDefault();
                        {
                            var ds = item1.GetBlockData(null);
                            //Console.WriteLine("Duration1={0},Duration2={1}", item.TimeCode + item1.TimeCode - l1, seg.Tracks[0].DefaultDuration / seg.Info.TimecodeScale);

                            if (item1 is Block)
                            {
                                var b = item1 as Block;

                                //Console.WriteLine("{0}-{1}，ref{2}", item.TimeCode + b.TimeCode, item.TimeCode + b.TimeCode + b.BlockDuration,b.ReferenceBlock);
                            }
                            else
                            {

                            }
                            l1 = item.TimeCode + item1.TimeCode;
                        }
                    }
                }
                //string str = "FF00 0100 C05D0000 E02E0000 0400 1000 0500 130856E598";
                //string str2 = "FF00 0200 80BB0000 E02E0000 0400 1000 0200 1190";
                //string str3 = "FF00 0200 44AC0000 20620500 0400 1000 0200 1210";
                Console.ReadLine();
            }
        }


        static void TestRegex()
        {
            string srcstr = @"{\fad(0,428)\pos(271.698113,583.5)}来我家里吧{\fs37\pos(658.113208,22.5)}澄空学园 x 华盟字幕社"; // 测试源
            string partten = @"\{\\[\w\W-[\{\}]]+\}[\w\W-[\{\}]]+"; //获取匹配项
            string partten1 = @"\{\\[\w\W-[\{\}]]+\}";  //获取控制器组
            string partten2 = @"\\[\w\W-[\\\}]]+";   //获取控制器
            string partten3 = @"\\[a-zA-Z]+";    //获取命令
            var ms = Regex.Matches(srcstr, partten);
            if (ms.Count > 0)
            {
                foreach (Match item in ms)
                {
                    var txt = Regex.Replace(item.Value, partten1, "");
                    Console.WriteLine("匹配原文：{0}", item.Value);
                    Console.WriteLine("文本：{0}", txt);
                    var ms2 = Regex.Matches(item.Value, partten2);
                    foreach (Match item2 in ms2)
                    {
                        Console.WriteLine("匹配控制器：{0}", item2.Value);
                        var ms3 = Regex.Matches(item2.Value, partten3);
                        var txt2 = Regex.Replace(item2.Value, partten3, "");
                        foreach (Match sub1 in ms3)
                        {
                            Console.WriteLine("匹配控制器命令：{0}", sub1.Value);
                        }
                        Console.WriteLine("匹配控制器参数：{0}", txt2);
                    }
                }
            }
            else
            {
                Console.WriteLine("没有匹配的子串");
            }
            Console.ReadLine();
        }
    }
}
